<template>
  <el-menu default-active="1-4-1" class="el-menu-vertical-demo" @open="handleOpen" @close="handleClose"
           :collapse="isCollapse" background-color="#545c64" text-color="#fff" active-text-color="#ffd04b">
    <!-- 要放到导航栏里面 -->
    <h3>{{ isCollapse ? "后台" : "通用后台管理系统" }}</h3>
    <!-- 观察数据,我们发现name是唯一标识 -->
    <!-- 查看文档,index是唯一标识 -->
    <el-menu-item class="el-menu-items" @click="clickItem(item)" v-for="item in noChildren" :key="item.menuName" :index="item.menuName">
      <!-- 这里是字体图标,用模板字符串拼接,注意要动态绑定 -->
      <component class="icons" :is="item.menuIcon"></component>
      <span>{{ item.menuName }}</span>
    </el-menu-item>
    <el-sub-menu class="el-menu-items" v-for="item in hasChildren" :key="item.menuName" :index="item.menuName">
      <template #title>
        <component class="icons" :is="item.menuIcon"></component>
        <span>{{ item.menuName }}</span>
      </template>
      <el-menu-item-group class="el-menu-items-group"  v-for="subItem in item.children" :key="subItem.menuName">
        <el-menu-item @click="clickItem(subItem)" :index="subItem.menuName">
          <component class="icons" :is="subItem.menuIcon"></component>
          <span>{{ subItem.menuName }}</span>
        </el-menu-item>
      </el-menu-item-group>
    </el-sub-menu>
  </el-menu>
</template>

<style lang="less" scoped>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 100%;
}

.el-menu {
  height: 100vh;
  // Aside和Header之间没有边框缝隙
  background-color: #304156;
  border-right: none;

  h3 {
    text-align: center;
    line-height: 48px;
    color: #fff;
    font-size: 16px;
    font-weight: 400;
  }
}

.el-menu-items-group {
  background-color: #304156;
}
.el-menu:active {
  color: #ffd04b;
}
.icons{
  width: 18px;
  height: 18px;
  margin-right: 5px;
}
</style>

<script>
import cookie from 'js-cookie'
import {mapState} from "vuex";

export default {
  data() {
    return {};
  },
  methods: {
    handleOpen(key, keyPath) {
    },
    handleClose(key, keyPath) {
    },
    clickItem(item) {
      // 防止自己跳自己的报错
      if (this.$route.path !== item.routerPath && !(this.$route.path === '/home' && (item.routerPath === '/'))) {
        this.$router.push(item.routerPath)
      }
      // 面包屑
      this.$store.commit('SelectMenu', item)
    }
  },
  computed: {
    noChildren() {
      // 如果没有children则返回true,会被过滤器留下
      return this.MenuData.filter(item => !item.children)
    },
    hasChildren() {
      return this.MenuData.filter(item => item.children)
    },
    // 要放到计算属性,自动计算
    isCollapse() {
      return this.$store.state.tab.isCollapse
    },
    // 获取菜单
    MenuData() {
      return JSON.parse(cookie.get('menu')) || this.$store.state.tab.menu
    }
  }
}
</script>